package com.group56.view.userend.schedule_module;

import com.group56.GymApp;
import com.group56.factory.AppCMFactory;
import com.group56.model.dao.Impl.CoachDAOImpl;
import com.group56.model.dao.Impl.ScheduleDAOImpl;
import com.group56.model.entity.Schedule;
import com.group56.utils.FXMLSrcMenu;
import com.group56.utils.FXMLViewHandler;
import com.group56.view.FrameworkController;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import lombok.SneakyThrows;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class ScheduleListController implements Initializable {

    @FXML
    private VBox list;
    public Button book_button;
    public Button video_button;

    @FXML
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        int index=0;
        Schedule schedule;
        String customerID = GymApp.customerId;
        ScheduleDAOImpl scheduleDAO= new ScheduleDAOImpl();
        ArrayList<Schedule> schedules= scheduleDAO.query(customerID,"Customer");
        while (index<schedules.size()){
            schedule = schedules.get(index);
            if (schedule != null)
                list.getChildren().add(addSchedule(schedule,index++));
            else
                break;
        }
    }

    @FXML
    public Pane addSchedule(Schedule schedule, int i){

        Pane pane=new Pane();
        Button delete = new Button("Delete");
        if (i%2==1)
            pane.setStyle("-fx-background-color: orange;");
        else
            delete.setStyle("-fx-background-color: orange;");




        delete.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @SneakyThrows
            @Override
            public void handle(MouseEvent mouseEvent) {
                Alert alert = new Alert(Alert.AlertType.NONE,"The money will be back", new ButtonType("confirm", ButtonBar.ButtonData.YES));
                alert.show();
                new ScheduleDAOImpl().delete(schedule.getScheduleID());
                try {
                    FrameworkController appFramework = (FrameworkController) AppCMFactory.getCtrlrMediator().getController(FXMLSrcMenu.UserEnd.APP_FRAMEWORK.getSrc());
                    FXMLViewHandler.putViewInPane(appFramework.getContentPane(), FXMLViewHandler.getView(FXMLSrcMenu.UserEnd.SCHEDULE_LIST.getSrc()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //            Calendar now = Calendar.getInstance();
                //            now


            }});


        Label label1=new Label("Coach: "+new CoachDAOImpl().selectByID(schedule.getCoachID()).getName());
        Label label2=new Label("Date: "+schedule.getDate());
        Label label3=new Label("Time: "+schedule.getTime());


        delete.setLayoutX(10);
        delete.setLayoutY(25);
        pane.getChildren().add(delete);
        label1.setLayoutX(100);
        label1.setLayoutY(25);
        label1.setPadding(new Insets(0,0,25,0));
        pane.getChildren().add(label1);
        label2.setLayoutX(250);
        label2.setLayoutY(25);
        label2.setPadding(new Insets(0,0,25,0));
        pane.getChildren().add(label2);
        label3.setLayoutX(420);
        label3.setLayoutY(25);
        label3.setPadding(new Insets(0,0,25,0));
        pane.getChildren().add(label3);
        return pane;
    }
}